<?php
/*
CREATE TABLE `blocks` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(100) collate utf8_lithuanian_ci NOT NULL,
  `info` text collate utf8_lithuanian_ci NOT NULL,
  `type` int(1) NOT NULL,--Ar file ar info
  `order` int(3) NOT NULL,
  `side` varchar(3) collate utf8_lithuanian_ci NOT NULL,
  `display` int(1) NOT NULL,
  `access` int(1) NOT NULL,
  `style_tbl` int(1) NOT NULL,
  `style_function` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci;
*/

//Sužinom kiek bus blokų grupių ir koki tų grupių užvadinimai kurie bus naudojami šalbone!
$sides = array();
$sql = $db->uzklausa("SELECT SQL_CACHE `side` FROM `".LENTELES_PRIESAGA."blocks` WHERE `access`<='".LYGIS."' AND `display`=1  GROUP BY `side`");
while ($row = $db->masyvas($sql)) {
	$sides[] = $row['side'];
}
$db->free_result($sql);
$irasu = count($sides);

// Sudarom visus block'us ir jų grupes ir nustatom grupių kintamajį šablonui (Šablone naudojamų block'ų grupių pavadinimus sužinojome anksčiau)
for($i = 1; $i <= $irasu; $i++) {
	if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $laikass1 = getmicrotime();
	// Nuskaitom grupės block'us
	$q = $db->uzklausa("SELECT SQL_CACHE * FROM `".LENTELES_PRIESAGA."blocks` WHERE `side`='".$sides[$i]."' AND `access`<='".LYGIS."' AND `display`=1 ORDER BY `side`,`order` ASC",__FILE__,__LINE__);
	$blockas = "";
	while ($row = $db->masyvas($q)) {
		if($row['type'] == 1) {//Jeigu failas vykdom
			if (is_file("block/".$row['info']."/".$row['info'].".php")) {
				if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $laikass = getmicrotime();
				if ($row['style_tbl'] == 0) {
					require("block/".$row['info']."/".$row['info'].".php");
					$blockas .= $this_block_result;
					unset($this_block_result,$this_block_title);
				} else {
					if(empty($row['style_function'])) {//Bloko styliaus funkcija nenurodyta, breziam default lentele
						require("block/".$row['info']."/".$row['info'].".php");
						if(!empty($this_block_result)) {
							$title = (isset($this_block_title)) ? $this_block_title : $row['title'];//Jeigu bloke nustatytas blocko title jį paliekam jei ne tada iš DB jis bus
							$blockas .=  style_block($title, $this_block_result);
						}
						unset($this_block_result,$this_block_title);
					} else {//Bloko stiliaus funkcija nurodyta, breziam nurodyta lentele
						require("block/".$row['info']."/".$row['info'].".php");
						if(!empty($this_block_result)) {
							$title = (isset($this_block_title)) ? $this_block_title : $row['title'];//Jeigu bloke nustatytas blocko title jį paliekam jei ne tada iš DB jis bus
							eval("\$blockas .= ".$row['style_function']."(".$title.", ".$this_block_result.");");
						}
						unset($this_block_result,$this_block_title);
					}
				}
				if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $blockas .= "<font color=\"red\">Block'ą per: ".round(getmicrotime()-$laikass,4)." s.</font>";
			} else {
				echo style_klaida("Klaida", "Tokio bloko/failo nėra failų sistemoje", $row['file']);
			}
		} else {//Tai ne failas vykdom eval(); arba echo "";
			if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $laikass = getmicrotime();
			if($row['style_tbl'] == 0) {
				eval("?>".$row['info']."<?php");
			} else {
				if(empty($row['style_function'])) {
					$blockas .= style_block($row['title'], $row['info']);
				} else {
					eval("\$blockas .= ".$row['style_function']."(".$row['title'].", ".$row['info'].");");
				}
			}
			if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $blockas .= "<font color=\"red\">Block'ą per: ".round(getmicrotime()-$laikass,4)." s.</font>";
		}
	}
	$db->free_result($q);
	$tpl->setVar($sides[$i], $blockas);
	unset($blockas);
	if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) echo "<font color=\"red\">Block'ų grupę (".$sides[$i].") per: ".round(getmicrotime()-$laikass1,4)." s.</font>";
}
unset($irasu);









/*
CREATE TABLE `panels` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(100) collate utf8_lithuanian_ci NOT NULL,
  `content` varchar(50) collate utf8_lithuanian_ci NOT NULL,
  `type` int(1) NOT NULL,
  `order` int(3) NOT NULL,
  `side` varchar(3) collate utf8_lithuanian_ci NOT NULL,
  `display` int(1) NOT NULL,
  `access` int(1) NOT NULL,
  `tbl` int(1) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci AUTO_INCREMENT=7 ;
*/
/*
class blocks {
	public function block($which) {
		global $db, $cache, $conf, $ses, $tbl;
		ob_start();
		$q = $db->uzklausa("SELECT * FROM `".LENTELES_PRIESAGA."panels` WHERE `side`='".$db->fix($which)."' AND `access`<='".LYGIS."' AND `display`=1 ORDER BY `order` ASC",__FILE__,__LINE__);
		while ($row = $db->masyvas($q)) {
			if (is_file("block/".$row['content'])) {
				if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $laikass = getmicrotime();
				if ($row['type'] == 1) { // File
					if ($row['tbl'] == 1) {
						echo lentele($row['title'], require("block/".$row['content'])); 
					} else {
						require("block/".$row['content']);
					}
				}
				if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) echo "<font color=\"red\">Generavimas užtruko: ".round(getmicrotime()-$laikass,4)." s.</font>";
			} else {
				echo lentele("Klaida","Tokio bloko nėra",$row['file']);
			}
		}
		$result = ob_get_contents();
		ob_end_clean();
		return $result;
	}
}
*/
/*
class blocks {
	private $blocks_data = array();
	private $blocks_num = array();

	public function __construct() {
		global $db;
		$result = $db->query("SELECT * FROM ".EMOCIJU_TABLE);
		$this->IsvisoSmile = $db->rows($result);
		if ($this->IsvisoSmile != 0) {
			$this->emocijos = $db->all_masyvas($result);
		}
		$this->emocija($info);
		$block = $db->uzklausa("SELECT * FROM `".LENTELES_PRIESAGA."panel` WHERE `align`='L' ORDER BY `place` ASC",__FILE__,__LINE__);
		while ($row_p = $db->masyvas($kaire_puse)) {
			if (is_file("block/".$row_p['file'])) {
				//if ($row_p['show'] == 'Y') { lentele($row_p['panel'], require "blokai/".$row_p['file']); }
				//else { require("blokai/".$row_p['file']); }
				if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) $laikass = getmicrotime();
				require ("block/".$row_p['file']);
				if (defined("DEBUG_EXTRA") && DEBUG_EXTRA) echo "Generavimas užtruko: ".round(getmicrotime()-$laikass,4)." s.";
			} else {
				lentele("Klaida","Tokio bloko nėra",$row_p['file']);
			}
		}
		return '';
	}

	public function block($info) {
		if ($this->IsvisoSmile == 0) {
			$this->GetSmile($info);
		}
		if ( !empty($this->emocijos) && ($this->IsvisoSmile != 0) ) {
			
			//while ($d = $this->info) {
			//	$info = ereg_replace($d['code'], '<img src="images/smilies/'.$d['smile_url'].'" alt="'.$d['emoticon'].'" border="0" />', $info);
			//}
			
			for ($i = 0; $i < $this->IsvisoSmile; $i++) {
				$info = ereg_replace($this->emocijos[$i]['code'], '<img src="images/smilies/'.$this->emocijos[$i]['smile_url'].'" alt="'.$this->emocijos[$i]['emoticon'].'" border="0" />', $info);
				//ereg_replace
			}
			
		}
		return $info;
	}
}
*/
?>
